package com.example.myanimation;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MenuActivity extends AppCompatActivity {

    @BindView(R.id.menu)
    Button menu;
    @BindView(R.id.item1)
    Button item1;
    @BindView(R.id.item2)
    Button item2;
    @BindView(R.id.item3)
    Button item3;
    @BindView(R.id.item4)
    Button item4;
    @BindView(R.id.item5)
    Button item5;

    private boolean isOpen = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.menu, R.id.item1, R.id.item2, R.id.item3, R.id.item4, R.id.item5})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.menu:
                menu.setClickable(false);
                if (isOpen == false) {
                    doOpenAnimator(item1, 0, 5, 700);
                    doOpenAnimator(item2, 1, 5, 700);
                    doOpenAnimator(item3, 2, 5, 700);
                    doOpenAnimator(item4, 3, 5, 700);
                    doOpenAnimator(item5, 4, 5, 700);
                } else {
                    doCloseAnimator(item1, 0, 5, 700);
                    doCloseAnimator(item2, 1, 5, 700);
                    doCloseAnimator(item3, 2, 5, 700);
                    doCloseAnimator(item4, 3, 5, 700);
                    doCloseAnimator(item5, 4, 5, 700);
                }
                break;
            case R.id.item1:
                Toast.makeText(this, "点击了：item1" + item1.getX() + "...." + item1.getY(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.item2:
                Toast.makeText(this, "点击了：item2" + item2.getX() + "...." + item2.getY(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.item3:
                Toast.makeText(this, "点击了：item3" + item3.getX() + "...." + item3.getY(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.item4:
                Toast.makeText(this, "点击了：item4" + item4.getX() + "...." + item4.getY(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.item5:
                Toast.makeText(this, "点击了：" + item5.getX() + "...." + item5.getY(), Toast.LENGTH_SHORT).show();
                break;
        }
    }

    private void doOpenAnimator(View view, final int index, final int total, int radius) {
        System.out.println("doOpenAnimator");

        if (view.getVisibility() == View.GONE) {
            view.setVisibility(View.VISIBLE);
        }
//         1.求出角度所对应的弧度值double toRadians(double angdeg)，因为Math中的
//          double sin(double d)，double cos(double d)，double tan(double d)参数都是角度所对的弧度值
        double d = Math.toRadians(90 / (total - 1) * index);
//            double d = Math.toRadians(90)/(total - 1) * index;;
        // 2.求sin，cos
        int x = -(int) (radius * Math.sin(d));
        int y = -(int) (radius * Math.cos(d));
        AnimatorSet set = new AnimatorSet();
        set.playTogether(
                ObjectAnimator.ofFloat(view, "translationX", 0, x),
                ObjectAnimator.ofFloat(view, "translationY", 0, y),
                ObjectAnimator.ofFloat(view, "scaleX", 0f, 1f),
                ObjectAnimator.ofFloat(view, "scaleY", 0f, 1f),
                ObjectAnimator.ofFloat(view, "alpha", 0f, 1f)

        );
        set.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                if (index == total - 1) {
                    menu.setClickable(true);
                }
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        set.setDuration(500);
        set.start();
        isOpen = true;
    }

    private void doCloseAnimator(final View view, final int index, final int total, int radius) {

        System.out.println("doCloseAnimator");
//         1.求出角度所对应的弧度值double toRadians(double angdeg)，因为Math中的
//          double sin(double d)，double cos(double d)，double tan(double d)参数都是角度所对的弧度值
        double d = Math.toRadians(90 / (total - 1) * index);
//            double d = Math.toRadians(90)/(total - 1) * index;;
        // 2.求sin，cos
        int x = -(int) (radius * Math.sin(d));
        int y = -(int) (radius * Math.cos(d));

        double degree = Math.PI * index / ((total - 1) * 2);
        int translationX = -(int) (radius * Math.sin(degree));
        int translationY = -(int) (radius * Math.cos(degree));
        AnimatorSet set = new AnimatorSet();
        set.playTogether(
                ObjectAnimator.ofFloat(view, "translationX", translationX, 0),
                ObjectAnimator.ofFloat(view, "translationY", translationY, 0),
                ObjectAnimator.ofFloat(view, "scaleX", 1f, 0.1f),
                ObjectAnimator.ofFloat(view, "scaleY", 1f, 0.1f),
                ObjectAnimator.ofFloat(view, "alpha", 1f, 0.1f)

        );
        set.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                if (view.getVisibility() == View.VISIBLE) {
                    view.setVisibility(View.GONE);
                }
                if (index == total - 1) {
                    menu.setClickable(true);
                }
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        set.setDuration(500);
        set.start();
        isOpen = false;

    }
}
